module(..., package.seeall)
-- load data from json file
local json= require "json"
local jsonFile = function( filename, base )

	-- set default base dir if none specified
	if not base then base = system.ResourceDirectory; end

	-- create a file path for corona i/o
	local path = system.pathForFile( filename, base )

	-- will hold contents of file
	local contents

	-- io.open opens a file at path. returns nil if no file found
	local file = io.open( path, "r" )
	if file then
	   -- read all contents of file into a string
	   contents = file:read( "*a" )
	   io.close( file )	-- close the file after using it
	end

	return contents
end
local jsonLoader=json.decode(jsonFile("test.json"))
local jsonLoaderMap=json.decode(jsonFile("map.json"))
-- split element to get data
function split2(str,c)
a = string.find(str,c)
b = string.find(str,c,a+1)
aCount = 0
start = 2
array = {}
	
	while b do
	array[aCount] = string.sub( str, start, b - 1 )
	start = b
	b = string.find(str, "}",b+1)
        str = string.gsub(str,"}", "", 1)
	aCount = aCount + 1
	end
	
return array
end
function split1(str, c)
str = string.gsub(str, "{", "", 1)
a = string.find(str, c)
aCount = 0
start = 1
array = {}
last = 0
	
	while a do
	array[aCount] = string.sub(str, start, a - 1)
        str=string.gsub(str,",","",1)
	start = a
	a = string.find(str, "}")
	str = string.gsub(str, "}", "", 1)
	aCount = aCount + 1
	end
	
return array
end
-- load data 
function loaddata(name)
    --get frame data
    orginalFrameData=jsonLoader.frames[name].frame
    frameData=split2(orginalFrameData,",")[0]
    x=split1(frameData,",")[0]
    y=split1(frameData,",")[1]
    frameSizeData=split2(orginalFrameData,",")[1]
    width=split1(frameSizeData,",")[0]
    height=split1(frameSizeData,",")[1]
    return x,y,width,height
end

function getdata(name)
    --print (name)
    x=jsonLoader.frames[name].x
    y=jsonLoader.frames[name].y
    width=jsonLoader.frames[name].width
    height=jsonLoader.frames[name].height
    return x,y,width,height
end

function getdata1(name)
    --print (name)
    x=jsonLoaderMap.frames[name].x
    y=jsonLoaderMap.frames[name].y
    return x,y
end